﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using  System.Windows;


public partial class TestTemp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void Button1_Click(object sender, EventArgs e)
    {


        //  //WebClient webClient = new WebClient();
        //  //var url = "http://tcstore1.17usoft.com/jqggnr/_bn2070001a-79f9-43af-985d-fb509b2f649dsz123456.pdf?AWSAccessKeyId=ZBUF77KQ1V90A3DW6QRA&Expires=2347689793&Signature=r7Rg3BEuGTCOuLApwJ17x4vnRWA=";
        //  var  url = "https://s.ly.com/UFsLf";
        //  this.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode("zyczyc.pdf")));

        //  this.Response.Clear();
        //  string jumpUrl = string.Empty;
        //  Stream str = client.OpenRead(URLAddress);
        //  StreamReader reader = new StreamReader(str);
        //  byte[] mbyte = new byte[1000000];
        //  int allmybyte = (int)mbyte.Length;
        //  int startmbyte = 0;
        //  HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        //  int responseStatus = Convert.ToInt32(response.StatusCode);
        //  if (responseStatus == 200)
        //  {
        //      if (!string.IsNullOrEmpty(loginUrl))
        //      {
        //          //HttpContext.Current.Response.Redirect(loginUrl, true);

        //  while (allmybyte > 0)
        //          byte[] bFile = webClient.DownloadData(loginUrl);
        //          if (bFile.Length < 5000)
        //  {
        //              return;
        //          }
        //          Response.Clear();
        //          Response.Buffer = true;
        //          Response.Charset = "UTF-8";
        //          Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
        //          Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");//utf-8
        //          Response.ContentType = "application/pdf";
        //          Response.BinaryWrite(bFile);
        //          Response.Flush();
        //          Response.End();

        //      int m = str.Read(mbyte, startmbyte, allmybyte);
        //      if (m == 0)
        //          break;

        //      startmbyte += m;
        //      allmybyte -= m;
        //  }
        //  }
        //  #endregion





        //  reader.Dispose();
        //  str.Dispose();
        //  string path = receivePath + System.IO.Path.GetFileName(URLAddress);
        //  FileStream fstr = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);

        //  this.Response.BinaryWrite();
        //  this.Response.End();
        //}
    }

    /// <summary>  
    /// Files the content.  
    /// </summary>  
    /// <param name="fileName">Name of the file.</param>  
    /// <returns></returns>  
    private byte[] FileContent(string fileName)
    {
        FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
        try
        {
            byte[] buffur = new byte[fs.Length];
            fs.Read(buffur, 0, (int)fs.Length);

            return buffur;
        }
        catch (Exception ex)
        {
            return null;
        }
        finally
        {
            if (fs != null)
            {

                //关闭资源  
                fs.Close();
            }
        }
    }

    public byte[] StreamToBytes(Stream stream)
    {
        byte[] bytes = new byte[stream.Length];
        stream.Read(bytes, 0, bytes.Length);
        // 设置当前流的位置为流的开始
        stream.Seek(0, SeekOrigin.Begin);
        return bytes;
    }

    private byte[] getFileBytes()
    {
        FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("文件名"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        byte[] DefaultByte = new byte[(int)fs.Length];
        fs.Read(DefaultByte, 0, (int)fs.Length);
        fs.Close();
        return DefaultByte;
    }




    //public static void downLoad(String _url)// throws IOException,FileNotFoundException
    //{
    //     URL url = new URL(_url);
    //         HttpURLConnection connection =(HttpURLConnection)url.openConnection();
    //         DataInputStream in = new DataInputStream(connection.getInputStream());
    //         String name = FileUtil.getUrlFileName(_url);
    //         log.info("url 的文件名为："+name);
    //         String newName = Properties.getString("HTML2PDF_Pilot_path")+"/temp/"+name+".doc";
    //         log.info("本地文件路径为："+newName);
    //         DataOutputStream out=new DataOutputStream(new FileOutputStream(newName));
    //         byte[] buffer = new byte[4096];
    //         int count = 0;
    //         while ((count = in.read(buffer)) > 0) {
    //             out.write(buffer, 0, count);
    //         }
    //         out.close();
    //         in.close();
    //}
}